<?php
declare (strict_types = 1);

namespace app\api\command\v1;

use app\api\model\v1\TemporaryFolder;
use think\cache\driver\Redis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;

class DelAlbum extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('delAlbum')
            ->setDescription('the delAlbum command');
    }

    protected function execute(Input $input, Output $output)
    {
        $config_redis = Config::get('redis');
        $redis = new Redis($config_redis);
        halt($redis);

        // 指令输出
        TemporaryFolder::chunk(100, function ($folders) use ($output) {
            foreach ($folders as $folder) {
                $data = $folder->toArray();
                $output->writeln(sprintf('Deleting current folder ID IS - '.$data['id'].'-'.$data['temporary_folder_name'].'的图片'));
                try {
                    $folder_res = TemporaryFolder::with('album')->find($data['id']);
                    $folder_res->together(['album'])->delete();

                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
        });
        $output->writeln('Delete complete');
        sleep(1); //睡眠1秒
    }
}
